# -*- coding: utf-8 -*-
# @Time    : 2020/12/18 15:07
# @Author  : YAQIWE
# @File    : ReqUtil.py
# @Software:  PyCharm
# @explain : 网络请求工具类

import random
import time
from urllib.parse import urlencode

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

import logging


class ReqUtil:
    pass

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
        'Referer': None,
    }

    userAgent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'

    retryNum = 3
    WAITING_TIME = 10
    STATUS_CODE = 200
    TOO_FAST = 503
    NONE_CODE = 404

    def getRequest(self, url, isProxies=False, timeout=50, headers=None, isNewProxies=False):
        """
        网络请求工具类
        :param url: 请求url
        :param isProxies: 是否使用代理请求
        :param timeout: 请求超时时间
        :param headers: 请求头
        :param isNewProxies: 是否更新代理请求
        :return:
        """
        reqNum = 0
        # 获取代理
        proxies = None
        TOO_FAST_NUM=0
        if isProxies :
            proxies = self.getProxies(isNew=isNewProxies)
        while reqNum < self.retryNum:
            reqNum += 1
            try:
                logging.debug(f'请求url:{url}')
                if None == headers:
                    headers = self.headers
                headers['User-Agent'] = self.userAgent
                headers['Referer'] = url
                requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
                res = requests.get(url=url, headers=headers, proxies=proxies, timeout=timeout, stream=True,
                                   verify=False)
                staCode = res.status_code
                # 请求成功返回数据
                if staCode == self.STATUS_CODE:
                    logging.debug(f'请求成功返回')
                    return res
                # 请求过快暂停一段时间
                elif staCode == self.TOO_FAST:
                    logging.debug(f'请求速度过快，等等时间{self.WAITING_TIME}后重新尝试')
                    time.sleep(self.WAITING_TIME)
                    TOO_FAST_NUM+=1
                    # 显示请求过快超过3次更换ip重新开始请求
                    if TOO_FAST_NUM == 3 and isProxies:
                        logging.debug('显示请求过快超过3次更换ip重新开始请求')
                        proxies = self.getProxies(isNew=True)
                        reqNum =0
                elif staCode == self.NONE_CODE:
                    logging.info('找不到资源请求结束')
                    break
            except Exception as e:
                logging.error(f'请求过程中发生错误:{e}')
                if isProxies:
                    proxies = self.getProxies(isNew=True)
                    reqNum -= 1
            if reqNum == self.retryNum:
                logging.error(f'超过重试次数{self.retryNum},重试结束')
        return -1

    paramas = {
        'orderid': '970395265553341',
        'num': 1,
        'pt': 1,
        'format': 'json',
        'sep': 1,
        'dedup': 1,
        'area': '4403,4401,4404,4405,4406'
    }
    getIpUrl = 'https://dps.kdlapi.com/api/getdps/'
    proxies = {}

    def getProxies(self, ipNumber=1, isNew=False):
        """
        获取付费ip列表
        :return:
        """
        if isNew:
            self.paramas['num'] = ipNumber
            data = urlencode(self.paramas)
            url = self.getIpUrl + '?' + data
            res = requests.get(url=url)
            if res.status_code == 200:
                info = res.json()
                if info['code'] == 0:
                    ip = info["data"]["proxy_list"]
                    logging.info(f'获取代理ip：{ip}')
                    return self.uploadProxies(ip)
                else:
                    logging.error(f'请求获取代理错误，code:{info["code"]}')

        return self.proxies

    def uploadProxies(self, ipList):
        """
        更新代理ip设置
        :return:
        """
        userName = '15338230927'
        password = 'ynbuh4hv'
        ip = random.choice(ipList)
        self.proxies = {
            "http": f"http://{userName}:{password}@{ip}/",
            "https": f"https://{userName}:{password}@{ip}/"
        }.copy()
        logging.debug(f'代理地址:{self.proxies}')
        return self.proxies
